package com.burgeon.backend.Rest.User;
/**
 * 定时任务获取会员数据
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.User;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetVipDataByRunner extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetVipDataByRunner(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<User> getData() {
        String url = "http://"+ip+"/servlets/binserv/Rest";

        try {
            String str = RESULT(Data(),url);
            System.err.println(str);
            String code = new JSONArray(str).getJSONObject(0).getString("code");
            String message = new JSONArray(str).getJSONObject(0).getString("message");
            if (!"0".equals(code)){
               throw new Exception(message);
            }
            JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
            ArrayList<User> data = new ArrayList<User>();
            for (int i =0;i<jsonArray.length();i++){
                JSONArray array = jsonArray.getJSONArray(i);
                User user = new User();
                user.setId(array.getString(0));
                user.setType(array.getString(1));
                user.setPhone(array.getString(2));
                user.setDiscount(array.getString(3));
                user.setIsEmp(array.getString(4).equals("null")?"N":array.getString(4));
                user.setIsPlus(array.getString(5).equals("PLUS会员")?"Y":"N");
                data.add(user);
            }

            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<User>();
        }
    }

    private MultiValueMap Data() {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("'C_VIPTYPE_ID;NAME'");
        columns.add("CARDNO");
        columns.add("'C_VIPTYPE_ID;DISCOUNT'");
        columns.add("'C_V_EMPLOYEE10_ID;NAME'");
        columns.add("IS_PLUSVIP");

        params.put("columns", columns);
        params.put("table", "C_VIP");//表名

        params.put("count","true");


        HashMap paramss = new HashMap();
        HashMap expr1= new HashMap();
        HashMap expr2= new HashMap();

        expr2.put("column", "'VIPSTATE'");
        expr2.put("condition", "Y");

        HashMap expr1v1= new HashMap();

        expr1v1.put("column", "OPENID2");
        expr1v1.put("condition", "is not null");
        expr1.put("expr1",expr1v1);

        HashMap expr1v2= new HashMap();

        expr1v2.put("column", "'ISACTIVE'");
        expr1v2.put("condition", "Y");
        expr1.put("expr2",expr1v2);
        expr1.put("combine","and");

        paramss.put("combine", "and");//关系
        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);
        //*************************************

        params.put("params", paramss);
        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        GetVipDataByRunner getRetailByVip = new GetVipDataByRunner("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(getRetailByVip.getData());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
